#!/bin/sh

if [ -f /etc/cvmfs/config.sh ]
then
  . /etc/cvmfs/config.sh
else
  echo "Failed to source /etc/cvmfs/config.sh"
  exit 1
fi

cvmfs_readconfig
if [ $? -ne 0 ]; then
  echo "Failed to read CernVM-FS configuration"
  exit 1
fi

fqrn=`cvmfs_mkfqrn $1`
org=`cvmfs_getorg $fqrn`

# Check for fuse
if [ ! -c /dev/fuse ]; then
  echo "Fuse not loaded"
  exit 1
fi

mountpoint=$2 
shift 2

cvmfs_readconfig $fqrn
retval=$?
if [ $retval -eq 1 ]; then
  echo "Failed to read CernVM-FS configuration"
  exit 1
fi
if [ $retval -eq 2 ]; then
  if [ "x$CVMFS_STRICT_MOUNT" == "xyes" ]; then
    echo "Not allowed to mount $fqrn, add it to CVMFS_REPOSITORIES"
    exit 1
  fi
fi
if [ -z $CVMFS_HTTP_PROXY ]; then
  echo "CVMFS_HTTP_PROXY required"
  exit 1
fi
existing_mountpoint=`cvmfs-talk -i $fqrn mountpoint 2>&1`
if [ $? -eq 0 ]; then
  echo "Repository $fqrn is already mounted on $existing_mountpoint"
  exit 1
fi

repository=`echo $CVMFS_SERVER_URL | sed "s/@org@/$org/g" | sed "s/@fqrn@/$fqrn/g"`
if [ x$CERNVM_CDN_HOST != x ] 
then
  mdir=`echo $repository |  cut -d/ -f4-`
  repository="$repository,http://$CERNVM_CDN_HOST/$mdir"
fi
name=$fqrn

ulimit -n $CVMFS_NFILES
mkdir -p $CVMFS_CACHE_DIR
chown $CVMFS_USER:$CVMFS_USER $CVMFS_CACHE_DIR
dryrun=

QUOTA="quota_limit=$CVMFS_QUOTA_LIMIT"
if [ $CVMFS_QUOTA_LIMIT -gt 0 ]
then
  QUOTA="$QUOTA,quota_threshold="$[$CVMFS_QUOTA_LIMIT/2]
fi

uid=`id -u $CVMFS_USER 2> /dev/null` && gid=`id -g $CVMFS_USER 2> /dev/null`

if [ $? -eq 0 ]
then
   CVMFS_ID=",uid=$uid,gid=$gid"
else
   echo "Error: $CVMFS_USER does not exist." 
   exit 1
fi

mount_options="-o fsname=cvmfs2,ro,nodev,grab_mountpoint,kernel_cache,auto_cache"
mount_options="${mount_options}${CVMFS_ID},cachedir=$CVMFS_CACHE_DIR,$QUOTA,$CVMFS_OPTIONS,proxies='$CVMFS_HTTP_PROXY',repo_name=$name"

if [ ! -z $CVMFS_TIMEOUT ]; then
   mount_options="$mount_options,timeout=$CVMFS_TIMEOUT"
fi
if [ ! -z $CVMFS_TIMEOUT_DIRECT ]; then
   mount_options="$mount_options,timeout_direct=$CVMFS_TIMEOUT_DIRECT"
fi
 

while getopts "vfo:" OPTION
do
  case $OPTION in
    v)
    ;;
    f)
      dryrun="true"
    ;;
    o)
      mount_options="${mount_options},$OPTARG"
    ;;
    ?)
      echo "Invalid option"
      exit 1
    ;;
  esac
done

cvmfs_exe=cvmfs2
if [ "x$CVMFS_DEBUGLOG" != "x" ]; then
  cvmfs_exe=cvmfs2_debug
  mount_options="${mount_options},logfile=$CVMFS_DEBUGLOG"
fi
if [ "x$CVMFS_TRACEFILE" != "x" ]; then
  mount_options="${mount_options},tracefile=$CVMFS_TRACEFILE"
fi
if [ "x$CVMFS_SYSLOG_LEVEL" != "x" ]; then
  mount_options="${mount_options},syslog_level=$CVMFS_SYSLOG_LEVEL"  
fi
if [ "x$CVMFS_FORCE_SIGNING" == "xyes" ]; then
  mount_options="${mount_options},force_signing"
fi
if [ "x$CVMFS_PUBLIC_KEY" != "x" ]; then
  mount_options="${mount_options},pubkey=$CVMFS_PUBLIC_KEY"
fi


cmd="$cvmfs_exe ${mount_options} $mountpoint '$repository'"
getent group fuse > /dev/null
if [ $? -eq 0 ]; then
  jump_group=y
else
  jump_group=n
fi

if [ "x$dryrun" == "xtrue" ]; then
  if [ $jump_group == "y" ]; then
    echo -e -n sg fuse -c \"
    echo -n $cmd
    echo -e \"
  else
    echo $cmd
  fi
  exit 0
else
  if [ $jump_group == "y" ]; then
    sg fuse -c "$cmd"
  else
    $cmd
  fi
  retval=$?
fi

if [ $retval -eq 0 ]; then
  sed -i "s|^cvmfs2 $mountpoint fuse \(.*\)|cvmfs2 $mountpoint cvmfs \1|" /etc/mtab
fi

exit $retval

